// pages/Jcrqxq/Jcrqxq.js
import ajax from '../../utils/api.js';
import dateChangeObj from '../../utils/util.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isAdd:false,
    signdate: '', //填单日期
    nowdate:'',//检测日期
    hiddenName: false,
    jcryObject: {}, //检测人员
    arrList: [],
    selectZhujiianNum: null,
    detailObj: {
      number: '', //统一受理编号
      title: '', //申请单位（委托单位）
      signdate: '', //填单日期
      nowdate:'',
      relaman: '', //联系人
      relatel: '', //联系电话
      itemName: '', //工程名称
      projAdr: '', //设备安装地点
      machinespec: '', //设备型号规格
      machnumber: '', //备案编号
      oldmainman: '', //原委托检测主检人员
      oldmainmanid: '',
      nowmainman: '', //现委托检主检人员
      nowmainmanid: '',
      oldminorman: '', //原委托检测辅检人员
      oldminormanid: '',
      nowminorman: '', //现委托检辅检人员
      nowminormanid: '',
      reason: '', //备注
    },
    jcid1: {},
    jcid2: {},
    isDisabled: false,//查看
    isEdit: false,//编辑
    isAudit:false//审批
  },
  // 选择检测人员
  xuanzhe: function (e) {
    this.setData({
      hiddenName: !this.data.hiddenName,
      sortpeo: e.currentTarget.dataset.sortpeo,
      selectZhujiianNum: null
    })
    // 主检人员
    if (this.data.sortpeo == 1) {
      this.setData({
        arrList: this.data.jcryObject[103]
      })
    }
    // 辅检人员
    if (this.data.sortpeo == 2) {
      this.setData({
        arrList: this.data.jcryObject[104]
      })
    }
  },

  // 关闭人员选择弹窗
  closeModel: function () {
    this.setData({
      hiddenName: !this.data.hiddenName,
      sortpeo: null,
      jcid1: {},
      jcid2: {}
    })
  },

  // 填单日期监听
  bindDateChange: function (e) {
    this.setData({
      signdate: e.detail.value
    })
  },
  // 申请更改检测日期监听
  bindNowdateChange: function (e) {
    this.setData({
      nowdate: e.detail.value
    })
  },
  // 联系人监听
  bindTextRelamanBlur:function (e) {
    var _this=this
    _this.setData({
    ['detailObj.relaman']:e.detail.value
    })
},
  // 联系电话监听
  bindTextRelatelBlur:function (e) {
    var _this=this
    _this.setData({
    ['detailObj.relatel']:e.detail.value
    })
},
  // 备注监听
  bindTextReasonBlur:function (e) {
    var _this=this
    _this.setData({
    ['detailObj.reason']:e.detail.value
    })
},
  //确认选择人员
  confirmjc: function (e) {
    if (this.data.sortpeo == 1 && this.data.jcid1.operatorname) {
      this.setData({
        ['detailObj.nowmainman']: this.data.jcid1.operatorname,
        ['detailObj.nowmainmanid']: this.data.jcid1.operatorid
      })
    }
    if (this.data.sortpeo == 2 && this.data.jcid2.operatorname) {
      this.setData({
        ['detailObj.nowminorman']: this.data.jcid2.operatorname,
        ['detailObj.nowminormanid']: this.data.jcid2.operatorid
      })
    }
    this.setData({
      hiddenName: !this.data.hiddenName,
      sortpeo: null,
      jcid1: {},
      jcid2: {}
    })
  },
  // 人员选择
  zhujianSelect: function (e) {
    // debugger
    this.setData({
      selectZhujiianNum: e.currentTarget.dataset.index
    })
    // 主检人员
    if (this.data.sortpeo == 1) {
      this.setData({
        jcid1: e.currentTarget.dataset.item
      })
    }
    // 辅检人员
    if (this.data.sortpeo == 2) {
      this.setData({
        jcid2: e.currentTarget.dataset.item
      })
    }
  },
  weitoList(id) { //获取检测人员
    var _this = this
    ajax.huoqury(id).then((res) => {
      _this.setData({
        jcryObject: res.data
      })
    }).catch((err) => {
      console.log(err)
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    });
  },
  // 获取当前用户信息
  getUserInfo(){
    var _this = this
    ajax.getInitInfo().then((res) => {
      _this.setData({
        ['detailObj.title']:res.data.title,
        ['detailObj.relaman']:res.data.linkman,
        ['detailObj.relatel']:res.data.mobile,
      })
    }).catch((err) => {
      console.log(err)
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    });
  },


  getDetail(id) { //获取详情
    var _this = this
    ajax.getJcryInfo(id).then((res) => {
      let form = {
        nowmainmanid: res.data.nowmainmanid,
        nowmainman: res.data.nowmainman,
        nowminormanid: res.data.nowminormanid,
        nowminorman: res.data.nowminorman,
        id: res.data.id,
        number: res.data.number,
        signdate: res.data.signdate,
        olddate:dateChangeObj.formatTimeTwo(res.data.olddate, 'Y-M-D'),
        nowdate: res.data.nowdate,
        title: res.data.title,
        relaman: res.data.relaman,
        relatel: res.data.relatel,
        itemName: res.data.itemName,
        projAdr: res.data.projAdr,
        machinespec: res.data.machinespec,
        machnumber: res.data.machnumber,
        checkid: res.data.checkid,
        reason: res.data.reason,
        changtype: "1",
      }
      _this.setData({
        detailObj: form,
        signdate: dateChangeObj.formatTimeTwo(res.data.signdate, 'Y-M-D'),
        nowdate:dateChangeObj.formatTimeTwo(res.data.nowdate, 'Y-M-D'),
      })
      _this.weitoList(res.data.checkid)
    }).catch((err) => {
      console.log(err)
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    });
  },
  getChecInfo(number){
    var _this = this
    ajax.getTentrustChecInfo(number).then((res) => {
      let form = {
        nowmainmanid: res.data.nowmainmanid,
        nowmainman: res.data.nowmainman,
        nowminormanid: res.data.nowminormanid,
        nowminorman: res.data.nowminorman,
         id: '',
        number: res.data.number,
        signdate: res.data.signdate,
        olddate:res.data.reqDate?dateChangeObj.formatTimeTwo(res.data.reqDate, 'Y-M-D'):'',
        nowdate: res.data.nowdate,
        title: res.data.title,
        relaman: res.data.relaman,
        relatel: res.data.relatel,
        itemName: res.data.itemName,
        projAdr: res.data.projAdr,
        machinespec: res.data.machinespec,
        machnumber: res.data.machnumber,
        checkid: res.data.id,
        reason: res.data.reason,
        changtype: "1",
      }
      _this.setData({
        detailObj: form,
        signdate:res.data.signdate? dateChangeObj.formatTimeTwo(res.data.signdate, 'Y-M-D'):dateChangeObj.formatTimeTwo(new Date(), 'Y-M-D'),
        nowdate:res.data.nowdate?dateChangeObj.formatTimeTwo(res.data.nowdate, 'Y-M-D'):dateChangeObj.formatTimeTwo(new Date(), 'Y-M-D'),
      })
      _this.weitoList(res.data.id)
      _this.getUserInfo()
    }).catch((err) => {
      console.log(err)
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    });
  },

  // 保存
  toSave() {
    var form = this.data.detailObj
    form.signdate = this.data.signdate //填单日期
    form.nowdate= this.data.nowdate //检测日期
    this.submitForm(form, 0)
  },
  // 提交
  toSubmit() {
    var form = this.data.detailObj
    form.signdate = this.data.signdate //填单日期
    form.nowdate= this.data.nowdate //检测日期
    this.setData({
      hiddenName: false
    })
    this.submitForm(form, 1)
  },
  // 不通过
  toReturn(){
    var form = this.data.detailObj
    form.signdate = this.data.signdate //填单日期
    form.nowdate= this.data.nowdate //检测日期
    this.submitForm(form, -1)
  },
    // 通过
    toPass(){
      var form = this.data.detailObj
      form.signdate = this.data.signdate //填单日期
      form.nowdate= this.data.nowdate //检测日期
      this.submitForm(form, 2)
    },
    // 保存/提交/审批
  submitForm(data, type) { 
    let form = data
    let _this=this
    // 保存/提交校验
    if (type == 0 || type == 1) {
      if (!form.signdate) {
        wx.showToast({
          title: '请选择填单日期！',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if (!form.nowdate) {
        wx.showToast({
          title: '请选择申请更的改检测日期！',
          icon: 'none',
          duration: 2000
        })
        return
      }
    }
    let text = '保存?';
    if (type == 1) text = "提交?";
    if (type == -1) text = "审批回退?";
    if (type == 2) text = "审批通过?";
    form.ischeck = type
    wx.showModal({
      title: '',
      content: '是否确定' + text,
      success(res) {
        if (res.confirm) {
          ajax.jcrySubmit(form).then((res) => {
            wx.showToast({
              title: '操作成功！',
              icon: 'none',
              duration: 1000
            })
            setTimeout(() => {
              wx.navigateBack({
                delta:_this.data.isAdd?2: 1
              })
            }, 1000)
          }).catch((err) => {
            console.log(err)
            wx.showToast({
              title: err.message,
              icon: 'none',
              duration: 2000
            })
          });
        }
      }
    })
    // ajax.jcrySubmit({
    //   form
    // }).then((res)=>{
    //     _this.setData({
    //         hqwtjcsList:res.data,
    //     })
    //     wx.showToast({
    //         title: res.data,
    //         icon: 'none',
    //         duration: 2000
    //     })
    //     wx.navigateBack({
    //         delta: 1
    //       })
    //     } ).catch((err) => {
    //         console.log(err)
    //       })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     // 根据id获取详情
     if (options && options.id) {
      this.setData({
        isAdd: false,
      })
      this.getDetail(options.id)
    }
    // 根据number获取详情(新增)
    if (options && options.number) {
      this.setData({
        isAdd: true,
      })
      this.getChecInfo(options.number)
    }
    if (options && options.isDisabled) {
      this.setData({
        isDisabled: options.isDisabled,
      })
    }
    if (options && options.isEdit) {
      this.setData({
        isEdit: options.isEdit,
      })
    }
    if (options && options.isAudit) {
      this.setData({
        isAudit: options.isAudit,
      })
    }
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})